//
//  BLTimeAxisRule.m
//  BLTimeAxis
//
//  Created by dg on 2025/4/17.
//

#import "BLTimeAxisRule.h"

@implementation BLTimeAxisRule

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSDate *date = [NSDate date];
        NSInteger h = [self fetchCurrentTimeValueWithUnit:NSCalendarUnitHour date:date];
        NSInteger m = [self fetchCurrentTimeValueWithUnit:NSCalendarUnitMinute date:date];
        NSInteger s = [self fetchCurrentTimeValueWithUnit:NSCalendarUnitSecond date:date];
        
        _currentTimeInterval =  h * 3600 + m * 60 + s;
    
//        _currentTimeInterval = [[NSDate date] timeIntervalSince1970]; // 秒数
        _axisDirection = BLTimeAxisDirectionHorizontal;
    }
    return self;
}

- (void)acceptVisitor:(id<BLTimeAxisVisitor>)visitor {
    [visitor visitTimeAxisRule:self];
}

- (NSDate *)currentHourDate {
    NSDateComponents *dateComponent = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:[NSDate dateWithTimeIntervalSince1970:self.currentTimeInterval]];
    dateComponent.minute = 0;
    dateComponent.second = 0;
    dateComponent.nanosecond = 0;
    return [[NSCalendar currentCalendar] dateFromComponents:dateComponent];
}

- (NSInteger)currentHour {   
    return self.currentTimeInterval / 3600;
//    return [self fetchCurrentTimeValueWithUnit:NSCalendarUnitHour];
}

- (NSInteger)fetchCurrentTimeValueWithUnit:(NSCalendarUnit)unit {
    return [[NSCalendar currentCalendar] component:unit fromDate:[NSDate dateWithTimeIntervalSince1970:self.currentTimeInterval]];
}

- (NSInteger)fetchCurrentTimeValueWithUnit:(NSCalendarUnit)unit date:(NSDate *)date {
    return [[NSCalendar currentCalendar] component:unit fromDate:date];
}

@end
